package aries;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Zip {

    private File filename;
    
    /**
     * Constructor.
     */
    public Zip(String filename) {
        this.filename = new File(filename);
    }

    /**
     * Constructor.
     * 
     * @param filename 
     */
    public Zip(File filename) {
        this.filename = filename;
    }
    
    /**
     * Create a new zip file.
     * 
     * @param zippedFile
     * @throws IOException 
     */
    public void create(String[] zippedFile) throws IOException {
        if (filename.exists()) {
            throw new IOException(filename+" is exist");
        }
        FileOutputStream fos = new FileOutputStream(filename);
        ZipOutputStream zos = new ZipOutputStream(fos);
        int bytesRead;
        byte[] buffer = new byte[1024];
        CRC32 crc = new CRC32();
        for (int i=0; i<zippedFile.length; i++) {
            String name = zippedFile[i];
            File file = new File(name);
            if (!file.exists()) {
                System.err.println("Skipping: " + name);
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            crc.reset();
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }
            bis.close();
            // Reset to beginning of input stream
            bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(name);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(file.length());
            entry.setSize(file.length());
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
            bis.close();
        }
        zos.close();
    }

    /**
     * Extract the content of zip file.
     */
    public void extract() throws IOException {
        ZipFile zipFile = new ZipFile(this.filename.getName());
        Enumeration enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            int size;
            byte[] buffer = new byte[2048];
            FileOutputStream fos = new FileOutputStream(zipEntry.getName());
            BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
            while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
                bos.write(buffer, 0, size);
            }
            bos.flush();
            bos.close();
            fos.close();
            bis.close();
        }
    }
    
    /**
     * Get the list content of zip file.
     * 
     * @return Object[]
     * @throws IOException 
     */
    public Object[] listContent() throws IOException {
        ZipFile zipFile = new ZipFile(filename);
        Enumeration list = zipFile.entries();
        ArrayList<String> theList = new ArrayList<String>();
        while (list.hasMoreElements()) {
            theList.add(((ZipEntry) list.nextElement()).getName());
        }
        return theList.toArray();
    }
    
}
